/**
 * Created by cuppi on 2017/6/8.
 */

import GlobalConstant from './GlobalConstant';
import {ACCOUNT} from '../vuex/mutations-types';
// import Network from '../network/Network';

let manager = null;
class AccountManager {
  constructor() {
    global.loginCallback = this.loginCallback.bind(this);
    this.curResolve = null;
  }

  static defaultManager() {
    if (!manager) {
      manager = new AccountManager();
    }
    return manager;
  }

  loginCallback(param) {
    if (this.curResolve) {
      this.curResolve(param);
    }
  }

  setAccount(account) {
    return new Promise((resolve, reject) => {
      try {
        let accountJson = JSON.stringify(account);
        window.localStorage.setItem('account', accountJson);
        resolve(account);
      } catch (error) {
        // Error saving dat
        console.log('LocalStorageManager - setAccount error: ' + error);
        reject(error);
      }
    });
  }

  /**
   * 异步获取用户信息
   * @param pickType
   */
  getSecret(pickType) {
  }

  /**
   * 同步获取用户信息
   * @param pickType 获取级别 {1: 从本地缓存获取(无弹窗), 2: 从本地vuex获取(无弹窗)}
   * @returns {*} 用户信息
   */
  syncGetAccount(pickType) {
    let vuexAccount = GlobalConstant.store.state.accountModule.account;
    if (vuexAccount) {
      return vuexAccount;
    }
    return null;
  }

  /**
   * 异步获取用户信息
   * @param pickType 获取级别 {0: 从原生获取(有弹窗), 1: 从本地缓存获取(无弹窗), 2: 从本地vuex获取(无弹窗)}
   * @param autoCache 是否自动缓存到全局
   * @returns {Promise}
   */
  asyncGetAccount(pickType, autoCache) {
    return new Promise((resolve, reject) => {
      try {
        // 这里先从同步获取数据
        let account = this.syncGetAccount(pickType);
        if (account) {
          resolve(account);
          return;
        }
        // 同步获取失败  采用异步获取
        if (pickType === 0) {
          this.curResolve = (accountSecret) => {
            GlobalConstant.store.commit(ACCOUNT.UPDATE_ACCOUNT, accountSecret);
            resolve(accountSecret);
          };
          if (GlobalConstant.isSimulateLogin){
            this.loginCallback('iTTzzyaE4tOT5upZf4OPkAMARuG6nPN4zN1317T3OmVq4UMzdvtZGAuTJVHteBLL/vOPxzr7xOCcF1VQg4L88z3K6CbaEg1DcTD3vM6oKF0n6Cx/uVSwRnpJStBtd8EhGTLJe/u3N07g5Fi8shxA9xIdcgOEAuo46euodKJFTuj7nivpnn4xT/WHZbFfj7NjwSUCVFbVgCtKBXJRFLbISr01bpr3Ck/WE4TP4LSRhCTW6yCi1KNZEpReDd8bwFLvx20p2FnqdR73Zygg67ZQDmGiZKeJZSEibOV81c11TyhOzXaNUaxyxJ7ek6GIGbX/CGuprXGQsJkG8sIsdS1kBJqc5XNaWwcSFdkTNUbotW+ee+U1Rq3H+9wBPthGiH8JxocoA6x+4Z5Ye7f/s6zkvA==');
          }
        } else {
          resolve(null);
        }
      } catch (error) {
        // Error retrieving data
        console.log('LocalStorageManager - getAccount error: ' + error);
        reject(error);
      }
    });
  }

  removeAccount() {
    return new Promise((resolve, reject) => {
      try {
        let account = this.syncGetAccount();
        window.localStorage.removeItem('account');
        resolve(account);
      } catch (error) {
        // Error saving dat
        console.log('LocalStorageManager - removeAccount error: ' + error);
        reject(error);
      }
    });
  }
}

export default AccountManager;
